package club.yuit.desktop.ui.topics;

import club.yuit.desktop.ui.topics.cut.CreateTopicDialog;
import club.yuit.rmq.service.RMQClusterService;
import lombok.extern.slf4j.Slf4j;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;

/**
 * @author: yuit
 * @date: 2023/12/08 16:45
 */
@Slf4j
public class TopicListControlPanel  extends JPanel {

    private TopicCardListPanel topicCardListPanel;
    private CustomCheckBox normalCheckBox;
    private CustomCheckBox sysCheckBox;
    private CustomCheckBox dlqCheckBox;
    private CustomCheckBox retryCheckBox;

    private JButton refreshBtn;
    private JButton addBtn;

    public static final int ALL_TYPE = 0b1111;
    public static final int NORMAL_TYPE = 0b1000;
    public static final int DLQ_TYPE = 0b0100;
    public static final int SYS_TYPE = 0b0010;
    public static final int RETRY_TYPE = 0b0001;

    private int showTypeValue = ALL_TYPE;

    private JComboBox<String> clusterBox;

    private RMQClusterService service;

    private String currentCluster = null;

    private JPopupMenu menu;



    public TopicListControlPanel(TopicCardListPanel topicCardListPanel,RMQClusterService service) {
        this.topicCardListPanel = topicCardListPanel;
        this.service = service;
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

        add(controlPanel());
        add(Box.createVerticalStrut(10));
        add(new JSeparator());

    }

    private JPanel controlPanel() {
        JPanel panel = new JPanel(){
            @Override
            public Insets getInsets() {
                return new Insets(0,0,0,0);
            }
        };
        panel.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 0));
        normalCheckBox = new CustomCheckBox("普通",NORMAL_TYPE);
        normalCheckBox.setSelected(true);
        sysCheckBox = new CustomCheckBox("系统",SYS_TYPE);
        sysCheckBox.setSelected(true);
        dlqCheckBox = new CustomCheckBox("死信",DLQ_TYPE);
        dlqCheckBox.setSelected(true);
        retryCheckBox = new CustomCheckBox("重试",RETRY_TYPE);
        retryCheckBox.setSelected(true);

        this.clusterBox = new JComboBox<>();
        panel.add(clusterBox);

        panel.add(normalCheckBox);
        panel.add(sysCheckBox);
        panel.add(dlqCheckBox);
        panel.add(retryCheckBox);

        addBtn = new JButton("添加");
        panel.add(addBtn);

        addBtn.addActionListener(e->{
            CreateTopicDialog dialog = new CreateTopicDialog(this, service);
            dialog.setVisible(true);
        });

        refreshBtn = new JButton("刷新");

        refreshBtn.addActionListener(e->{
            try {
                // 更行列表
                topicCardListPanel.updateTopicsUI(service);
                topicCardListPanel.updateUI();
                // 更新集群列表
                initClusterBox();
                normalCheckBox.setSelected(true);
                sysCheckBox.setSelected(true);
                dlqCheckBox.setSelected(true);
                retryCheckBox.setSelected(true);

            }catch (Exception ex) {
                ex.printStackTrace();
            }
        });
        panel.add(refreshBtn);


        checkBoxChang(normalCheckBox);
        checkBoxChang(sysCheckBox);
        checkBoxChang(dlqCheckBox);
        checkBoxChang(retryCheckBox);

        initClusterBox();

        this.clusterBox.addItemListener(e->{
            if(e.getStateChange() == ItemEvent.SELECTED){
                this.currentCluster = (String) e.getItem();
                try {
                    if (currentCluster.equals("全部")){
                        this.currentCluster = null;
                    }
                    topicCardListPanel.updateShowByTypeValue(showTypeValue,currentCluster);
                }catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });



        return panel;
    }


    public void initClusterBox() {

        try {
            clusterBox.removeAllItems();
            this.clusterBox.addItem("全部");
            service.getClusterList().forEach(clusterBox::addItem);
        }catch (Exception e) {
            e.printStackTrace();
        }
    }


    private void checkBoxChang(JCheckBox box){
        box.addItemListener(e -> {

            int stateChange = e.getStateChange();

                Object source = e.getSource();
                if (source instanceof CustomCheckBox cbx){
                    int value = cbx.getValue();
                    if(stateChange == ItemEvent.SELECTED){
                        showTypeValue|=value;
                    }else {
                       int v = value^ALL_TYPE;
                        showTypeValue &=v;
                    }

                    log.debug(Integer.toBinaryString(showTypeValue));

                    topicCardListPanel.updateShowByTypeValue(showTypeValue,currentCluster);
                }

        });

    }


    @Override
    public Insets getInsets() {
        return new Insets(10, 5, 0, 5);
    }

    public static void main(String[] args) {
        int i = 0b1000;
        int b = ~i;
        System.out.println(b);
        System.out.println(Integer.toBinaryString(b));
    }

}

class CustomCheckBox extends JCheckBox {
    private int value;
    public CustomCheckBox(String text,int value) {
        super(text);
        this.value = value;
    }

    @Override
    public Insets getInsets() {
        return new Insets(0, 0, 0, 0);
    }

    public int getValue() {
        return value;
    }
}
